package cn.wh.org.controller;

import cn.wh.basic.exception.BusinessException;
import cn.wh.basic.util.ExcelUtil;
import cn.wh.org.domain.ShopAuditLog;
import cn.wh.org.service.IShopService;
import cn.wh.org.domain.Shop;
import cn.wh.org.query.ShopQuery;
import cn.wh.basic.util.AjaxResult;
import cn.wh.basic.util.PageList;
import cn.wh.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;
    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.removeById(id);
            return AjaxResult.my();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    @DeleteMapping
    public AjaxResult removeAll(@RequestBody List<Long> ids){
        try {
            shopService.deleteAll(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(true).setMessage("弄给和hiGV橘郡");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }



    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody  Shop shop){
        try {
            if (shop.getId() == null){
                shopService.settlement(shop);
            }else{
                shop.setState(0);
                shopService.update(shop);
            }
            return AjaxResult.my();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }

    }

    /**
     * 通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.my();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }

    }

    /**
     * 驳回
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.my();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }

    /**
     * 导出excel
     * @param response
     */
    @GetMapping("/export/excel")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件导入
     * @param file
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
            List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
            //批量新增
            shopService.batchInsert(shops);
            System.out.println("导入的数据条数："+shops.size());
    }
    /**
     * 报表
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        List<ShopVo> voList = shopService.queryEcharts();
        List<String> states = voList.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> nums = voList.stream().map(ShopVo::getNum).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }
    /**
     * 激活
     * @param shopId
     * @return
     */
    @GetMapping("/activation/{shopId}")
    public String activation(@PathVariable("shopId") Long shopId){
        shopService.activation(shopId);
        return "激活成功";
    }

    /**
     * 驳回
     * @param adminId
     * @return
     */
    @GetMapping("/getById/{adminId}")
    public Shop findAdminById(@PathVariable("adminId") Long adminId){
        return shopService.findAdminById(adminId);

    }


}
